package ngames.com.sushitv.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import ngames.com.sushitv.R;
import ngames.com.sushitv.temp.Adapter.MyitemAnimator;
import ngames.com.sushitv.temp.Adapter.TagAdapter;
import ngames.com.sushitv.entity.Tag;
import ngames.com.sushitv.temp.Interface.TagInterface;
import ngames.com.sushitv.temp.Module.TagFractory;
import ngames.com.sushitv.temp.Presenter.TagPresenter;

/**
 * Created by edmond on 16-7-26.
 */
public class TagsFragment extends Fragment implements TagInterface {
    View view;

    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;

    private Activity activity;
    private RecyclerView.LayoutManager layoutManager;

    private List<Tag> tagsList = new ArrayList<>();
    private TagAdapter adapter;
    private TagPresenter presenter;

    private boolean isLoad = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_tags,container,false);
        activity = getActivity();
        layoutManager = new LinearLayoutManager(activity);
        TagFractory tagFractory = new TagFractory();
        presenter = new TagPresenter(this,tagFractory);

        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeRefreshLayout);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        adapter = new TagAdapter(activity,tagsList);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new MyitemAnimator());

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.refeshData();
            }
        });

        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int lastVisibleItem = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
                int totalItemCount = layoutManager.getItemCount();
                Log.d("AnimeFragment", String.valueOf(lastVisibleItem));
                Log.d("AnimeFragment", String.valueOf(totalItemCount));
                if(lastVisibleItem>=totalItemCount-1&&!isLoad){
                    isLoad = true;
                    Log.d("AnimeFragment","LoadMore");
                    presenter.moreData();
                }
            }
        });

        presenter.initData();

        return view;
    }

    @Override
    public void initingData() {
        adapter.notifyDataSetChanged();
    }

    @Override
    public void initedData() {

    }

    @Override
    public void refreshingData() {

    }

    @Override
    public void refreshedData() {
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void moreingData() {
    }

    @Override
    public void moredData() {
        Log.d("TagsFragment","dsfsd");
        adapter.notifyDataSetChanged();
        isLoad = false;
    }

    @Override
    public List<Tag> getTagsList() {
        return tagsList;
    }
}
